define(['jquery', 'bootstrap', 'backend', 'table', 'form','print'], function ($, undefined, Backend, Table, Form,undefined) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('提交人'), operate: 'LIKE'},
                        {field: 'depart', title: __('部门'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),searchList: {1: __('Status 1'), 2: __('status 2'),3: __('Status 3'), 4: __('status 4')},formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'nums', title: __('Nums')},
                        {field: 'sendtime', title: __('Sendtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),
                        buttons: [
                            {
                                name: '订单详情',
                                text: __('订单详情'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'order/detail'
                            }
                        
                        ],
                        table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            
            Controller.api.bindevent();
            var table =  $('#table', window.parent.document);
            console.log(table);
            var id = $('.order-id').val();
            $(document).on('click','.print',function(){
                console.log(13123);
                $(".panel-body").print()
                return
            })
            $(document).on('click','.send',function(){
                $.ajax({
                    url:'order/send',
                    data:{id},
                    dataType:'Json',
                    success:function(res){
                        layer.msg(res.msg)
                        if(res.code){
                            window.location.reload();
                            table.bootstrapTable('refresh');
                            // window.parent.location.reload()
                        }
                    }
                })
                return
            })
            $(document).on('click','.check',function(){
                // console.log(21313);
                // table.bootstrapTable('refresh');
                // return;
                $.ajax({
                    url:'order/check',
                    data:{id},
                    dataType:'Json',
                    success:function(res){
                        layer.msg(res.msg)
                        if(res.code){
                            window.location.reload();
                            table.bootstrapTable('refresh');
                            // window.parent.location.reload()
                        }
                    }
                })
                return
            })
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
